<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_CountrySettings extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('countrysetting');
	}
	public function ajax_countrysetting_get(){
		$countrysettingEntity =  new CI_Country_setting_entity();
		$countrysetting = $countrysettingEntity->init('id',$_REQUEST['id']);
		echo json_encode($countrysetting);
	}
	public function ajax_countrysetting_list(){		
		$countrysettingEntity =  new CI_Country_setting_entity();
		$countrysettingList = $countrysettingEntity->getCountrySettingList();
		
		$data['countrysettingList'] = $countrysettingList;
		
		$data['countrysettingCountryList'] = getCountryData_ioc();
		$ci = &get_instance();
		$paymentGateWayList = $ci->payment_gateway_entity->getPaymentGatewayList();
		$data['paymentGateWayList'] = array();
		if($paymentGateWayList){
			foreach ($paymentGateWayList as $paymentGateWay){
				$data['paymentGateWayList'][$paymentGateWay->id] = $paymentGateWay->gateway;
			}
		}
		
		$this->load->view('admin/countrysettings/index', $data);
	}
	public function ajax_countrysetting_save(){
		$result = array();
		$countrysettingEntity =  new CI_Country_setting_entity();
		if($_REQUEST['countrysetting_tax'] == null || $_REQUEST['countrysetting_tax'] == ""){
			$result['result'] = false;
			$result['message'] = lang('escort_countrysetting_require');
		}else{
			if($this->_check_countrysetting(0, $_REQUEST['country_id'])){
				$result['result'] = false;
				$result['message'] = lang('escort_countrysetting_existed');
			}else{
				$ci = &get_instance();
				$countryData = $ci->countries_entity->init('id', $_REQUEST['country_id']);
				$countrysettingEntity->setCountryId($_REQUEST['country_id']);
				$countrysettingEntity->setCountryCode($countryData->country_code);
				$countrysettingEntity->setTax($_REQUEST['countrysetting_tax']);
				if($this->input->post('payment_gateway') != null && count($_REQUEST['payment_gateway']) > 0){
					$paymentGateWayIds = implode(',', $_REQUEST['payment_gateway']);
				}else{
					$paymentGateWayIds = '';
				}
				$countrysettingEntity->setPaymentGatewayIds($paymentGateWayIds);
				
				$id = $countrysettingEntity->save();
				$result['result'] = true;
				$result['message'] =lang('escort_countrysetting_save_success');
				$result['id'] =$id;
			}
		}
		echo json_encode($result);
	}
	public function ajax_countrysetting_update(){
		$result = array();
		$countrysettingEntity =  new CI_Country_setting_entity();
		if($_REQUEST['countrysetting_tax'] == null || $_REQUEST['countrysetting_tax'] == ""){
			$result['result'] = false;
			$result['message'] = lang('escort_countrysetting_require');
		}else{
			if($this->_check_countrysetting($_REQUEST['countrysetting_id'], $_REQUEST['country_id'])){
				$result['result'] = false;
				$result['message'] = lang('escort_countrysetting_existed');
			}else{
				$ci = &get_instance();
				$countryData = $ci->countries_entity->init('id', $_REQUEST['country_id']);
				$countrysettingEntity->setId($this->input->post('countrysetting_id'));
				$countrysettingEntity->setCountryId($_REQUEST['country_id']);
				$countrysettingEntity->setCountryCode($countryData->country_code);
				$countrysettingEntity->setTax($_REQUEST['countrysetting_tax']);
				if($this->input->post('payment_gateway') != null && count($_REQUEST['payment_gateway']) > 0){
					$paymentGateWayIds = implode(',', $_REQUEST['payment_gateway']);
				}else{
					$paymentGateWayIds = '';
				}
				$countrysettingEntity->setPaymentGatewayIds($paymentGateWayIds);
				$countrysettingEntity->update();
				$result['result'] = true;
				$result['message'] = lang('escort_countrysetting_save_success');
			}
		}
		echo json_encode($result);
	}
	public function ajax_countrysetting_delete(){
		$result = array();
		$countrysettingEntity =  new CI_Country_setting_entity();
		$countrysettingEntity->delete($_REQUEST['id']);
		$result['message'] = lang('escort_countrysetting_delete_success');
		echo json_encode($result);
	}
	function _check_countrysetting($id, $country_id){
		$ci = &get_instance();
		return $ci->country_setting_entity->checkExistedCountry($id, $country_id);
	}
}
?>